import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () =>
  import ('../components/Home')
const Login = () =>
  import ('../components/Login')
const Welcome = () =>
  import ('../components/Welcome')
const Users = () =>
  import ('../components/user/Users')
const Rights = () =>
  import ('../components/rights/Rights')
const Roles = () =>
  import ('../components/rights/Roles')
const Cate = () =>
  import ('../components/goods/Cate')
const Params = () =>
  import ('../components/goods/Params')
const List = () =>
  import ('../components/goods/List')
const Add = () =>
  import ('../components/goods/Add')
const Edit = () =>
  import ('../components/goods/Edit')
const Orders = () =>
  import ('../components/order/Orders')
const Reports = () =>
  import ('../components/report/Reports')
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/', redirect: '/home' },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/goods/edit', component: Edit },
      { path: '/orders', component: Orders },
      { path: '/reports', component: Reports }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !window.sessionStorage.getItem('token')) {
    next('/login')
  }
  next()
})

export default router
